<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <div class="search-list-content">
        <van-list v-model="loading" :finished="done"  finished-text="没有更多了"
        @load="pullUpLoadingMoreFn"
        :immediate-check="false"
        offset="50">
                  <article-item v-for="item in resultList" :key="item.art_id" :isshowCross="false" :item="item"></article-item>
        </van-list>
      </div>
    </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api/search.js'
// 页码
let page = 1
export default {
  name: 'SearchResult',
  data () {
    return {
      resultList: [],
      loading: false,
      done: false
    }
  },
  async created () {
    this.getSearchResultList()
  },
  computed: {
    txt () {
      return this.$route.params.keyword
    }
  },
  methods: {
    // 获取搜索结果列表
    async getSearchResultList () {
      // 1.发请求
      const res = await getSearchResultListAPI({
        keyword: this.txt,
        page: page++
      })
      console.log(res)
      const { results } = res.data.data
      if (results.length < 10) {
        this.done = true
        return
      }
      // console.log(res)
      // this.resultList = res.data.data.results
      // 2.合并数据
      this.resultList = [...this.resultList, ...results]
      // 可以继续加载下一页
      this.loading = false
    },
    // 上拉加载更多
    pullUpLoadingMoreFn () {
      if (this.resultList.length > 0) {
        this.getSearchResultList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
